﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Windows.Forms.DataVisualization.Charting;

namespace UsageTracker.Reporting.Util
{
    using System.Drawing;

    public static class Utility
    {
        public static void DrawUsageDistribution(Dictionary<string, int> Usage, string ImagePath, string XAxisTitle, string YAxisTitle, string ChartTitle)
        {
            Chart chart1;
            chart1 = new Chart();
            chart1.Series.Add("Default");
            chart1.ChartAreas.Add("Default");
            chart1.BackColor = Color.White;
            chart1.ChartAreas["Default"].BackColor = Color.White;
            List<int> yValues = new List<int>();
            Usage.Keys.ToList().ForEach(
                k =>
                {
                    yValues.Add(Usage[k]);
                    
                });

            List<string> xValues = Usage.Keys.ToList();
            chart1.Series["Default"].Points.DataBindXY(xValues, yValues);

            chart1.BorderlineColor = Color.Transparent;
            chart1.Series["Default"].LabelForeColor = Color.Blue;
            chart1.Series["Default"]["LabelStyle"] = "Top";
            chart1.Series["Default"].IsValueShownAsLabel = true;
            chart1.Series["Default"].Font = new Font("Arial", 10, FontStyle.Regular);
            
            chart1.Series["Default"].ChartType = SeriesChartType.Column;
            chart1.Series["Default"].Palette = ChartColorPalette.Excel;
            chart1.Palette = ChartColorPalette.Bright;

            
             
            chart1.Titles.Add("Default");
            chart1.Titles[0].Text = ChartTitle;
            chart1.Titles[0].Alignment = ContentAlignment.TopCenter;
            chart1.Titles[0].Font = new Font("Arial", 11, FontStyle.Bold);
            chart1.Titles[0].BackColor = Color.White;

            chart1.Series[0].XAxisType = AxisType.Primary;
            chart1.Series[0].XValueType = ChartValueType.String;

            chart1.ChartAreas[0].AxisX.LabelStyle.Font = new Font("Arial", 10);
            chart1.ChartAreas[0].AxisX.LabelStyle.Angle = -45;
            chart1.ChartAreas[0].AxisX.Title = XAxisTitle;
            chart1.ChartAreas[0].AxisX.TitleFont = new Font("Arial", 10, FontStyle.Bold);
            chart1.ChartAreas[0].AxisY.Title = YAxisTitle;
            chart1.ChartAreas[0].AxisY.TitleFont = new Font("Arial", 10, FontStyle.Bold);
            
            chart1.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.FromArgb(40,150,150,150);
            chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.FromArgb(40, 150, 150, 150);

            chart1.ChartAreas[0].AxisX.Interval = 1;
            chart1.ChartAreas[0].AxisX.IsLabelAutoFit = false;

            chart1.ChartAreas[0].BorderWidth = 0;

            chart1.Size = new Size(650, 550);


            chart1.SaveImage(ImagePath, ChartImageFormat.Png);
        }
    }
}